//
//  StatusButtonView.m
//  RentTable
//
//  Created by mac on 2024/11/30.
//

#import "StatusButtonView.h"

@interface StatusButtonView ()

@property (nonatomic, strong) UIButton *auditButton;
@property (nonatomic, strong) UIButton *pendingButton;
@property (nonatomic, strong) UIButton *rentalButton;
@property (nonatomic, strong) UIButton *completedButton;

@property (nonatomic, strong) NSArray<UIButton *> *buttons;
@property (nonatomic, strong) UIButton *selectedButton;

@end

@implementation StatusButtonView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.auditButton = [self createButtonWithTitle:@"审核中"];
    self.pendingButton = [self createButtonWithTitle:@"待取货"];
    self.rentalButton = [self createButtonWithTitle:@"租赁中"];
    self.completedButton = [self createButtonWithTitle:@"已完成"];
    
    self.buttons = @[self.auditButton, self.pendingButton, self.rentalButton, self.completedButton];
    
    UIStackView *stackView = [[UIStackView alloc] initWithArrangedSubviews:self.buttons];
    stackView.axis = UILayoutConstraintAxisHorizontal;
    stackView.distribution = UIStackViewDistributionFillEqually;
    stackView.spacing = 0;
    stackView.layer.cornerRadius = 20;
    stackView.layer.masksToBounds = true;
    stackView.backgroundColor = myRGBHex(0xFFFFFF);
    [self addSubview:stackView];
    
    [stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    [self buttonTapped:self.auditButton];
}

- (UIButton *)createButtonWithTitle:(NSString *)title {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor whiteColor]];
    button.titleLabel.font = [UIFont pingFangSCRegularWithSize:15];
    [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
    return button;
}

- (void)buttonTapped:(UIButton *)sender {
    for (UIButton *button in self.buttons) {
        [button setBackgroundColor:[UIColor whiteColor]];
        [button setTitleColor:myRGBHex(0x999999) forState:UIControlStateNormal];
        button.layer.cornerRadius = 0;
        button.clipsToBounds = NO;
    }
    
    [sender setBackgroundColor:myRGBHex(0xFF0AA1)];
    [sender setTitleColor:myRGBHex(0xFFFFFF) forState:UIControlStateNormal];
    sender.layer.cornerRadius = 20;
    sender.clipsToBounds = YES;
    
    self.selectedButton = sender;
    
    if ([self.delegate respondsToSelector:@selector(didSelectButtonWithTitle:)]) {
        [self.delegate didSelectButtonWithTitle:sender.titleLabel.text];
    }
}


@end
